package org.example.chat.validate.validator;

import org.example.chat.validate.annotation.XSS;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Safelist;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * xss校验器
 * @author cheva
 */
public class XssValidator implements ConstraintValidator<XSS, String> {

    private static final Safelist whiteList = Safelist.relaxed();
    private static final Document.OutputSettings setting = new Document.OutputSettings().prettyPrint(false);


    @Override
    public boolean isValid(String content, ConstraintValidatorContext constraintValidatorContext) {
        String cleanContent = Jsoup.clean(content, "", whiteList, setting);
        return content.equals(cleanContent);
    }
}
